package com.td.server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import com.td.core.communication.obj.Message;

public class Listener extends Thread {
	
	private ServerSocket serverSocket;
	
	public Listener(int port) throws IOException {
			serverSocket = new ServerSocket(port);
	}
	
	/**
	 * creates new user with communication thread based on socket and
	 * adds user to list
	 */
	public void run(){
		
		ClientContainer cc = null;
		
		while(!interrupted()){
			try {
				Socket socket = serverSocket.accept();
				if(socket != null) {
					cc = new ClientContainer(socket);
					cc.start();
					cc.send(new Message(Message.PRE_GAME_INFO_REQUEST, ""));
					Server.getInstance().getQueue().add(cc);
					System.out.println("---------------");
					System.out.println("Client connected from ip: " + socket.getInetAddress());
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
